#!/usr/bin/python

# Imports
import os, pygame
import opencv.adaptors as adaptors

# Basepaths
BASEDIR = 'capture'
TMPDIR = '/tmp/capture'
DATADIR = 'data'

def load_image(filename):
    """Loads and returns an image with pygame.
    Parameters: string filename - location of the image to be loaded."""
    try:
        image = pygame.image.load(filename)
    except pygame.error:
        print "Cannot load image:", filename
    return image.convert_alpha()

def convert_image(img):
    """Converts an openCV image to a pygame image.
    Parameters: IplImage img - image"""
    array = adaptors.Ipl2NumPy(img)
    surf = pygame.surfarray.make_surface(array)
    surf = pygame.transform.rotate(surf, -90)
    surf = pygame.transform.flip(surf, 1, 0)
    return surf

def load_sound(filename):
    """Loads and returns a sound with pygame.
    Parameters: string filename - location of the sound to be loaded."""
    class NoneSound:
        def play(self): 
            pass
    if not pygame.mixer:
        return NoneSound()
    filename = os.path.join(DATADIR, filename)
    try:
        sound = pygame.mixer.Sound(filename)
    except pygame.error:
        print 'Cannot load sound: ',  filename
    return sound

def mouse_collision(rect):
    """Returns True if the position of the mouse is located within the given rectangle.
    Parameters: Rect rect - pygame rectangle of (usually) a button."""
    m_width = 10
    m_height = 10
    m_x, m_y = pygame.mouse.get_pos()
    
    if rect.x - m_width < m_x < rect.x + rect.width and rect.y - m_height < m_y < rect.y + rect.height:
        return True
